const app = getApp()
// pages/login/login.js
Page({

    /**
     * 页面的初始数据
     */
    data: {

        loginbtnstate: 'true', //登录按钮状态
        loginbtnstate2: 'true', //注册按钮状态
        registeriphone: '',
        registerpassword: '',
        strloginUser: '', //登录姓名
        strloginpassword: '', //用户密码
        switchChecked: '', //选中按钮
    },
    register: function () {
        wx.navigateTo({
            url: '/pages/register/register',
        })
    },
    //获取是否记住密码的状态值
    bindswitchchange: function (e) {
        console.log(e)
        var that = this
        var x = e.detail.value
        if (x == true) {
            that.setData({
                switchChecked: x,
            })
        } else if (x == false) {
            that.setData({
                switchChecked: x,
            })
        }
        console.log(that.data.switchChecked + '状态')
    },
    //登录手机号判断
    inputiphone1: function (e) {
        console.log(e.detail.value)
        var that = this
        var x = e.detail.value
        if (x != '' && (x.length == 12)) {
            that.setData({
                strloginUser: x
            })
            if (that.data.strloginpassword != '')
                that.setData({
                    loginbtnstate: false
                })
            else {
                that.setData({
                    loginbtnstate: true
                })
            }
        } else {
            that.setData({
                loginbtnstate: true
            })
        }
    },
    //登录密码判断
    password: function (e) {
        console.log(e.detail.value)
        var that = this
        var x = e.detail.value
        if (x != '') {
            that.setData({
                strloginpassword: x
            })
            if (that.data.strloginUser != '' && (that.data.strloginUser.length == 12)) {
                that.setData({
                    loginbtnstate: false
                })
            } else {
                that.setData({
                    loginbtnstate: true
                })
            }
        }
    },
    //登录
    onlogin: function (e) {
       
        var that = this

        var x = wx.getStorageSync('userjob2')
        //    if(that.data.strloginUser==x.iphone){
        //      if(that.data.strloginpassword==x.password){
           

        wx.request({
            url: app.globalData.requestContext + '/login',
            method: "post",
            data: {
                account: this.data.strloginUser,
                password: this.data.strloginpassword,

            },

            header: {
                'content-type': "application/x-www-form-urlencoded" // 默认值
            },
            success: function (result) {
                wx.removeStorageSync('sessionid'); //每次登录时清楚缓存
                if (result.data.code == 0) {
                    // wx.setStorageSync('sessionid', result.header['Set-Cookie']); //保存Cookie到Storage
                    wx.setStorageSync('token', result.data.data.token);
                    wx.showToast({
                        title: '登录成功',
                    })
                   
                    wx.switchTab({
                        url: '/pages/myself/myself',
                    })

                } else {
                    wx.showToast({
                        title: result.data.msg,
                        icon: "error"
                    })
                }
                console.log(result.data)
                console.log("success")
            }
        })


    },
    
   
    


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

      app.checkLogin()
  
    },



/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady() {

},

/**
 * 生命周期函数--监听页面显示
 */
onShow() {

},

/**
 * 生命周期函数--监听页面隐藏
 */
onHide() {

},

/**
 * 生命周期函数--监听页面卸载
 */
onUnload() {

},

/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
onPullDownRefresh() {

},

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom() {

},

/**
 * 用户点击右上角分享
 */
onShareAppMessage() {

}
})